package com.example.admin.SQLite.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.admin.SQLite.Complaint;
import com.example.admin.SQLite.SqLiteOpenHelper;
import com.example.admin.SQLite.YiJian;

import java.util.ArrayList;

import static com.example.admin.SQLite.SqLiteOpenHelper.YIJIAN;

public class YiJianDao {
    SqLiteOpenHelper dbHelper;
    private static final String KEY_ID = "id";

    public YiJianDao(Context context) {
        dbHelper = new SqLiteOpenHelper(context);
    }
    public long addData(YiJian bean) {
        SQLiteDatabase db = dbHelper.getSqlite();
        ContentValues values = new ContentValues();
        values.put("yijianContent", bean.yijianContent);
        values.put("yijianState", bean.yijianState);
        values.put("yijianTime", bean.yijianTime);
        values.put("uid", bean.uid);
        long id = db.insert(YIJIAN, null, values);
        return id;
    }

    public ArrayList<YiJian> getComPlanc(String uid) {
        Log.e("大师傅士大夫撒旦", "getComPlanc: " + uid);
        String selection = "uid=?"; // userName为要查询的列名
        String[] selectionArgs = {uid}; // value为要查询的值
        SQLiteDatabase db = dbHelper.getSqlite();
        Cursor cursor = db.query(YIJIAN, null, selection, selectionArgs, null, null, null);

        ArrayList<YiJian> arrContacts = new ArrayList<>();

        while (cursor.moveToNext()) {
            YiJian model = new YiJian();
            model.id = cursor.getInt(0);
            model.yijianContent = cursor.getString(1);
            model.yijianState = cursor.getString(2);
            model.yijianTime = cursor.getString(3);
            model.uid = cursor.getInt(4);

            arrContacts.add(model);

        }
        return arrContacts;
    }
    public void updateCom(int id, String str) {

        SQLiteDatabase database  = dbHelper.getSqlite();

        ContentValues cv = new ContentValues();
        cv.put("yijianState", str);

        database.update(YIJIAN, cv, KEY_ID + " = " + id, null);
    }
    public ArrayList<YiJian> fetchData() {

        SQLiteDatabase db = dbHelper.getSqlite();
        Cursor cursor = db.rawQuery("SELECT * FROM " + YIJIAN, null);

        ArrayList<YiJian> arrContacts = new ArrayList<>();

        while (cursor.moveToNext()) {
            YiJian model = new YiJian();
            model.id = cursor.getInt(0);
            model.yijianContent = cursor.getString(1);
            model.yijianState = cursor.getString(2);
            model.yijianTime = cursor.getString(3);
            model.uid = cursor.getInt(4);

            arrContacts.add(model);

        }
        return arrContacts;
    }

}
